using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_ProductBrandEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        ProductBrand productBrand;

        if (Int32.TryParse(Request["id"], out id))
            productBrand = ProductBrand.Load(id);
        else
            productBrand = null;

        if (productBrand != null)
        {
            if (!string.IsNullOrEmpty(productBrand.BrandName))
				tbxBrandName.Text = productBrand.BrandName;
			if (!string.IsNullOrEmpty(productBrand.BrandLogoImageUrl))
				tbxBrandLogoImageUrl.Text = productBrand.BrandLogoImageUrl;
			tbxBrandItemCount.Text = productBrand.BrandItemCount.ToString();
			if (productBrand.CreateDate != SqlDateTime.MinValue)
				tbxCreateDate.Text = productBrand.CreateDate.ToShortDateString();
			cbxIsFeaturedBrand.Checked = productBrand.IsFeaturedBrand;
			if (!string.IsNullOrEmpty(productBrand.SortToken))
				tbxSortToken.Text = productBrand.SortToken;

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string brandName = Security.BindStringInput(tbxBrandName.Text, _securityLevel);
		string brandLogoImageUrl = Security.BindStringInput(tbxBrandLogoImageUrl.Text, _securityLevel);
		int brandItemCount = Security.BindIntegerInput(tbxBrandItemCount.Text, _securityLevel);
		DateTime createDate = Security.BindDateTimeInput(tbxCreateDate.Text, _securityLevel);
		bool isFeaturedBrand = cbxIsFeaturedBrand.Checked;
		string sortToken = Security.BindStringInput(tbxSortToken.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (ProductBrand.Save(
				id, 
				brandName,
				brandLogoImageUrl,
				brandItemCount,
				createDate,
				isFeaturedBrand,
				sortToken
                ))
                    Response.Redirect("productbrandlist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            ProductBrand productBrand = ProductBrand.Create(
				brandName,
				brandLogoImageUrl,
				brandItemCount,
				createDate,
				isFeaturedBrand,
				sortToken
                );

            if (productBrand != null)
                Response.Redirect("productbrandlist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("productbrandlist.aspx");
    }
}
